//_hextobytes.h
//
/*
 This is the HDiffPatch copyright.

 Copyright (c) 2018-2025 HouSisong All Rights Reserved.

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
#ifndef HDiffPatch_hextobytes_h
#define HDiffPatch_hextobytes_h
#include "libHDiffPatch/HPatch/patch_types.h"

#ifdef __cplusplus
extern "C" {
#endif

static const char _halfbyte_to_hex1[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
static const char _halfbyte_to_HEX1[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

hpatch_inline static
void byte_to_hex2(hpatch_byte b,char out_hex2[2]){
    out_hex2[0]=_halfbyte_to_hex1[b>>4]; //h
    out_hex2[1]=_halfbyte_to_hex1[b&0xF];//l
}
hpatch_inline static
void byte_to_HEX2(hpatch_byte b,char out_HEX2[2]){
    out_HEX2[0]=_halfbyte_to_HEX1[b>>4]; //h
    out_HEX2[1]=_halfbyte_to_HEX1[b&0xF];//l
}

//out_hexs must have enough space for 2*blen
hpatch_inline static
void bytes_to_hexs(const hpatch_byte* bytes,hpatch_size_t blen,char* out_hexs){
    size_t i;
    for (i=0; i<blen; ++i)
        byte_to_hex2(bytes[i],&out_hexs[i*2]);
}
hpatch_inline static
void bytes_to_HEXs(const hpatch_byte* bytes,hpatch_size_t blen,char* out_HEXs){
    size_t i;
    for (i=0; i<blen; ++i)
        byte_to_HEX2(bytes[i],&out_HEXs[i*2]);
}

static const unsigned char _hex1_to_halfbyte[256] = {
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x08,0x09,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, //0-9
    0xFF,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, //A-F
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, //a-f
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};

#define __private_hex2_to_byte(hex_0,hex_1,rb)  \
    hpatch_byte h=_hex1_to_halfbyte[(unsigned char)(hex_0)];    \
    hpatch_byte l=_hex1_to_halfbyte[(unsigned char)(hex_1)];    \
    (rb)=(hpatch_byte)((h<<4)|l);

hpatch_inline static
hpatch_BOOL hex2_to_byte(const char hex2[2],hpatch_byte* out_b){
    __private_hex2_to_byte(hex2[0],hex2[1],*out_b);
    return ((h|l) <= 0xF);
}

//hexStrLen must be even, out_bytes must have enough space for hexStrLen/2 bytes
hpatch_inline static
hpatch_BOOL hexs_to_bytes(const char* hexs,hpatch_size_t hexStrLen,hpatch_byte* out_bytes){
    hpatch_size_t i;
    hpatch_size_t blen=hexStrLen>>1;
    hpatch_byte check=0;
    assert((hexStrLen&1)==0);
    for (i=0; i<blen; ++i){
        __private_hex2_to_byte(hexs[i*2],hexs[i*2+1],out_bytes[i]);
        check|=h; check|=l;
    }
    return (check <= 0xF);
}

#ifdef __cplusplus
}
#endif
#endif //HDiffPatch_hextobytes_h
